package GUI;
import BUS.ContactGroupBUS;
import DTO.ContactGroupDTO;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewGroupGUI.java
 *
 * Created on May 20, 2009, 10:47:11 AM
 */

/**
 *
 * @author trquanglong
 */
public class NewGroupGUI extends javax.swing.JDialog {

    /**
     * Local Variable: is button OK pressed ?
     */
    private String _nameGroup = "";

    /** Creates new form NewGroupGUI */
    public NewGroupGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNameGroup = new javax.swing.JTextField();
        bttnOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New contact group");

        jLabel1.setText("Group name:");

        txtNameGroup.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNameGroupKeyReleased(evt);
            }
        });

        bttnOk.setText("OK");
        bttnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNameGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bttnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                .addComponent(txtNameGroup, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(bttnOk))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel1.getAccessibleContext().setAccessibleName("New contact group");
        jPanel1.getAccessibleContext().setAccessibleDescription("New contact group");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bttnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnOkActionPerformed
        // TODO add your handling code here:
        String nameGroup = txtNameGroup.getText();
        if (nameGroup.equals("") == true)
            JOptionPane.showMessageDialog(null, "Please fill name of group!", "Error", JOptionPane.ERROR_MESSAGE);
        else{

            ArrayList <ContactGroupDTO> lstGroup = new ArrayList<ContactGroupDTO>();
            lstGroup = ContactGroupBUS.getListContactGroup();
            for (int i = 0; i < lstGroup.size(); i++)
                if (lstGroup.get(i).getNameGroup().equals(nameGroup)){
                    JOptionPane.showMessageDialog(null, "Name of group existed \nPlease type another name!", "Error", JOptionPane.ERROR_MESSAGE);
                    txtNameGroup.setText("");
                    return;
                }
            
            setNameGroup(txtNameGroup.getText());
            }
           this.dispose();
    }//GEN-LAST:event_bttnOkActionPerformed

      /**
     * Press Enter = Click button Ok
     * @param evt
     */
private void txtNameGroupKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNameGroupKeyReleased
        // TODO add your handling code here:
        java.awt.event.ActionEvent actEvent = null;
        if (evt.getKeyCode() == KeyEvent.VK_ENTER)
            this.bttnOkActionPerformed(actEvent);
        if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
            this.dispose();
}//GEN-LAST:event_txtNameGroupKeyReleased


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewGroupGUI dialog = new NewGroupGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bttnOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtNameGroup;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the _nameGroup
     */
    public String getNameGroup() {
        return _nameGroup;
    }

    /**
     * @param nameGroup the _nameGroup to set
     */
    public void setNameGroup(String nameGroup) {
        this._nameGroup = nameGroup;
    }

}
